<?php
/**
 * @version 0.8.3 2010-10-07
 * @package Joomla
 * @subpackage LeagueResults
 * @copyright (C)  2008, 2009, 2010 Henryk Drozd
 * @license GNU/GPL, see LICENSE.php
 * LeagueResults is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * LeagueResults is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport('joomla.application.component.controller');
jimport('joomla.filesystem.file');
jimport('joomla.filesystem.archive');

class lrControllerLr_restore extends lrController {

	function __construct()
	{
		parent::__construct();
	}

	function cancel()
	{
		// Check for request forgeries
		JRequest::checkToken() or die( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_lr' );
	}


	function Restore( )
	{
		
		global $mainframe;
		$_rows = 0;
		$msg = null;
		
		$withsettings = JRequest::getVar('withsettings');
		$conversion_otf = JRequest::getVar('conversion_otf');
		
		$bak_file = JPATH_SITE.DS.'media'.DS.JRequest::getVar('bak_file');
		$result = JArchive::extract($bak_file, JPATH_SITE.DS.'media');
		$text_bak_file = substr($bak_file, 0, strlen($bak_file)-3);
		
		$bquery = JFile::read($text_bak_file);
		$sss = strlen($bquery);
		switch ($conversion_otf){
			case 'iso-8859-2-to-UTF-8':
				$bquery = iconv("iso-8859-2", "utf-8", $bquery);
				break;
			
			default:
				break;
		}
		
		 		
		JFile::delete($text_bak_file);		
		$lr_database = & JFactory::getDBO();
		$lr_tables = array();

		if ($withsettings = 'checked'){
			$lr_database->setQuery ('TRUNCATE TABLE #__lr_settings');
			
			if (!$result = $lr_database->query()) {
	  			$msg .= $lr_database->stderr();
				}
		}
		$lr_database->setQuery ("SHOW TABLES LIKE '%_lr_%'");
		if (!$result = $lr_database->query()) {
	  			$msg .= $lr_database->stderr();
				}
		$lr_tables = $lr_database->loadRowList();
		
		foreach ($lr_tables as $t) {
			if (strpos($t[0], 'settings') == 0)
				$emptying_query .= 'TRUNCATE TABLE '.$t[0].';';
		}
		$lr_database->setQuery ($emptying_query);
			
			if (!$result = $lr_database->queryBatch()) {
	  			$msg .= $lr_database->stderr();
				}
		
		$lr_database->setQuery ($bquery);
		
		if (!$result = $lr_database->queryBatch()) {
  			$msg .= $result.$lr_database->stderr();
			}
			
		if ($result == 'true') 
    		$msg .= JText::_("\nRESTORE QUERIES EXECUTED SUCCESSFULLY!");
		else
			$msg .= JText::_("\nRESTORE QUERIES EXECUTION FAILED...");
		
				$mainframe->redirect('index.php?option='.JRequest::getVar('option').'&view=lr', $msg);
		
		parent::display();
	}
}
?>
